﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Moneyman.Library;
using Moneyman.DataModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Moneyman.ViewModel
{
    public class HomeViewModel : MainViewModel
    {
        public HomeViewModel()
        {
            newTransactionItemToAdd.TransactionChanged -= newTransactionItemToAdd_TransactionChanged;
            newTransactionItemToAdd.TransactionChanged += newTransactionItemToAdd_TransactionChanged;
            if (!IsDesignMode)
            {
                SetDefaultCategory();
            }
        }

        void newTransactionItemToAdd_TransactionChanged(object sender, TransactionInfo.TransactionKindArgs e)
        {
            if (e.PropertyName == "Kind")
            {
                RaisePropertyChanged(() => ShowCategoryCollection);
            }
            RaisePropertyChanged(() => AddNewTransactionCommand);
        }

        #region Properties
        
        private TransactionInfo newTransactionItemToAdd = new TransactionInfo();
        public TransactionInfo NewTransactionItemToAdd
        {
            get { return newTransactionItemToAdd; }
            set
            {
                newTransactionItemToAdd = value;
                RaisePropertyChanged(() => NewTransactionItemToAdd);
            }
        }

        private CategoryInfo selectedCategory;
        public CategoryInfo SelectedCategory
        {
            get { return selectedCategory; }
            set
            {
                selectedCategory = value;
                RaisePropertyChanged(() => SelectedCategory);
                NewTransactionItemToAdd.CategoryGuid = SelectedCategory.Guid;
                RaisePropertyChanged(() => NewTransactionItemToAdd);
            }
        }

        private CaseInfo selectedCase;
        public CaseInfo SelectedCase
        {
            get { return selectedCase; }
            set
            {
                selectedCase = value;
                RaisePropertyChanged(() => SelectedCase);
                if (SelectedCase != null)
                {
                    NewTransactionItemToAdd.CaseGuid = SelectedCase.Guid;
                }
                else
                {
                    NewTransactionItemToAdd.CaseGuid = null;
                }
                RaisePropertyChanged(() => NewTransactionItemToAdd);
            }
        }

        private bool linkToCase = false;
        public bool LinkToCase
        {
            get { return linkToCase; }
            set
            {
                linkToCase = value;
                RaisePropertyChanged(() => LinkToCase);
                if (LinkToCase && SelectedCase != null)
                {
                    NewTransactionItemToAdd.CaseGuid = SelectedCase.Guid;
                }
                else
                {
                    NewTransactionItemToAdd.CaseGuid = null;
                }
                RaisePropertyChanged(() => NewTransactionItemToAdd);
            }
        }
        #endregion

        #region Collections
        public ObservableCollection<CategoryInfo> ShowCategoryCollection
        {
            get
            {
                return new ObservableCollection<CategoryInfo>(CommonCategoryCollection.Where(c => c.Kind == NewTransactionItemToAdd.Kind));
            }
        }
        #endregion

        #region Commands
        public ICommand CancelAddCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        NewTransactionItemToAdd = new TransactionInfo();
                    }
                };
            }
        }

        public ICommand AddNewTransactionCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return NewTransactionItemToAdd.Amount > 0; },
                    ExecuteCallback = delegate
                    {
                        InsertTransaction(NewTransactionItemToAdd);
                        RaisePropertyChanged(() => AddNewTransactionCommand);
                    }
                };
            }
        }
        #endregion

        #region Method
        private void InsertTransaction(TransactionInfo entity)
        {
            if (!string.IsNullOrWhiteSpace(entity.Place) && GlobalSettings.AutoSavePlace)
            {
                if (CommonPlaceCollection.Count(p => p.Place == entity.Place) == 0)
                {
                    Database.CollectionOfPlaces.InsertOnSubmit(new PlaceInfo() { Place = entity.Place });
                    RaisePropertyChanged(() => CommonPlaceCollection);
                }
            }

            Database.CollectionOfTransactions.InsertOnSubmit(entity);
            Execute();

            NewTransactionItemToAdd = new TransactionInfo();
        }

        private void SetDefaultCategory()
        {
            if (ShowCategoryCollection != null && ShowCategoryCollection.Count > 0)
            {
                var item = ShowCategoryCollection.FirstOrDefault(c => c.Default);
                if (item == null)
                {
                    SelectedCategory = ShowCategoryCollection[0];
                }
                else
                {
                    SelectedCategory = item;
                }

                RaisePropertyChanged(() => SelectedCategory);
            }
        }
        #endregion
    }
}
